package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.ConsumptionItem;
import cn.wolfcode.ssm.service.IConsumptionItemService;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/consumptionItem")
public class ConsumptionItemController {

    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequireAnnotation(name = "结算单明细页面",expression = "consumptionItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<ConsumptionItem> pageInfo = consumptionItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumptionItem/list";
    }

    @RequestMapping("/delete")
    @RequireAnnotation(name = "结算单明细删除",expression = "consumptionItem:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            consumptionItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequireAnnotation(name = "结算单明细新增/编辑",expression = "consumptionItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ConsumptionItem consumptionItem){
        if (consumptionItem.getId() != null) {
            consumptionItemService.update(consumptionItem);
        }else {
            consumptionItemService.save(consumptionItem);
        }
        return new JsonResult();
    }
}
